﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using ActiveDs;
using System.DirectoryServices.AccountManagement;
using System.IO;
using System.Net.Mail;
using Infinite.Systems.Util.Library;
using Infinite.Systems.Util.Library.Base;
using System.Configuration;

namespace Infinite.Systems.Util.WAPasswordReminderUtil
{
    class Program
    {
               
        /// <summary>
        /// Main Program
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            
            //TODO: ALL configurable items to be placed in app.config
            string reminderDaysList = ConfigurationManager.AppSettings["DayRemainingAlertList"].ToString();
            int passwordPolicyDay = Convert.ToInt16(ConfigurationManager.AppSettings["PasswordPolicyDays"].ToString());
            string filter = ConfigurationManager.AppSettings["AdFilterQuery"].ToString();
            string ldapPath = ConfigurationManager.AppSettings["LdapPath"].ToString();

            try
            {
                Type adReminderType = Type.GetType("Infinite.Systems.Util.Library.ActiveDirectoryEmailReminder, Infinite.Systems.Util.Library");
                IActiveDirectoryReminder adReminder = (IActiveDirectoryReminder)Activator.CreateInstance(adReminderType, new object[] { ldapPath, filter, passwordPolicyDay, reminderDaysList });
                List<IADUser> adUserList = adReminder.FindAllForExpirationReminder();
                               
                if (Convert.ToBoolean(ConfigurationManager.AppSettings["AlertON"]) == true) 
                { 
                    adReminder.SendAlert(adUserList); 
                }
                
            }
            catch (Exception ex)
            {
                File.AppendAllText("Log.txt","Exception Occured in the System on " + DateTime.Now.ToLongDateString() + Environment.NewLine + ex.Message + Environment.NewLine)
                Console.WriteLine("Exception occured in the program : " + ex.Message);
            }
        }



    }

      



}
